//
//  ClassificationViewController.m
//  Fragrance
//
//  Created by dllo on 15/5/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ClassificationViewController.h"
#import "myImageView.h"
#import "CommonViewController.h"

#define RADIUS 80.0
#define PHOTONUM 6
#define PHOTOSTRING @"icon"
#define TAGSTART 1000
#define TIME 5
#define SCALENUMBER 1.25

int array [PHOTONUM][PHOTONUM] ={
    {0,1,2,3,4,5},
    {5,0,1,2,3,4},
    {4,5,0,1,2,3},
    {3,4,5,0,1,2},
    {2,3,4,5,0,1},
    {1,2,3,4,5,0}
};


@interface ClassificationViewController ()




@end

@implementation ClassificationViewController
CATransform3D rotationTransform1[PHOTONUM];

- (void)dealloc
{
    [_textArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor colorWithRed:119 / 255.0 green:211 / 255.0 blue:251 / 255.0 alpha:1.0];
    
    self.navigationItem.title = @"分类";
    
    mScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
    mScrollView.backgroundColor = [UIColor redColor];
    [self.view addSubview:mScrollView];

    for (int i = 0; i < 8; i++) {
        NSString *str = [NSString stringWithFormat:@"shiyong%d.jpg", i];
        UIImage *image = [UIImage imageNamed:str];
        UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, 150)];
        myImageView.image = image;
        [mScrollView addSubview:myImageView];
        [myImageView release];
        
        myImageView.contentMode = UIViewContentModeScaleAspectFill;


    }
    
    mScrollView.showsHorizontalScrollIndicator = NO;
    mScrollView.showsVerticalScrollIndicator = NO;
    
    mScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 8, 80);
    mScrollView.pagingEnabled = YES;
    mScrollView.bounces = NO;
    
    [mScrollView setContentOffset:CGPointMake(320, 0)];
    
    mScrollView.delegate = self;
    
    [mScrollView release];
    


    
    self.textArray = [NSArray arrayWithObjects:@"蛋糕甜点", @"生鲜果蔬", @"饮品酒茶", @"调味酱料", @"小吃零食",@"养生养颜", nil];
    float centery = self.view.center.y;
    float centerx = self.view.center.x;

    for (int i = 0; i < PHOTONUM; i++) {
        float tempy = centery + RADIUS * cos(2.0*M_PI * i / PHOTONUM);
        float tempx = centerx - RADIUS * sin(2.0*M_PI * i / PHOTONUM);
        myImageView *addview1 = [[myImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@%d", PHOTOSTRING, i]] text:[self.textArray objectAtIndex:i]];

        addview1.frame = CGRectMake(0.0, 0.0,120,50);
        [addview1 setdege:self];
        addview1.tag = TAGSTART + i;
        addview1.center = CGPointMake(tempx + 10,tempy);
        
        rotationTransform1[i] = CATransform3DIdentity;
        
        
        float Scalenumber = fabs(i - PHOTONUM/2.0)/(PHOTONUM/2.0);
        if (Scalenumber<0.3)
        {
            Scalenumber = 0.4;
        }
        CATransform3D rotationTransform = CATransform3DIdentity;
        rotationTransform = CATransform3DScale (rotationTransform, Scalenumber*SCALENUMBER,Scalenumber*SCALENUMBER, 1);
        addview1.layer.transform=rotationTransform;
        [self.view addSubview:addview1];
        
    }
    currenttag = TAGSTART;
 
    
    
}


#pragma mark - 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int offset = scrollView.contentOffset.x / self.view.frame.size.width;
    if (offset == 0) {
        [mScrollView setContentOffset:CGPointMake(self.view.frame.size.width * 6, 0) animated:NO];
        
    }else if(offset == 7){
        [mScrollView setContentOffset:CGPointMake(self.view.frame.size.width * 1, 0) animated:NO];
    }
}




- (void)Clickup:(NSInteger)tag
{

    
    NSInteger t = [self getBlank:tag];
    NSLog(@"-----%ld", t);
    NSInteger i = 0;
    for (i = 0; i < PHOTONUM; i++) {
        
        
        UIImageView *imagview = (UIImageView*)[self.view viewWithTag:TAGSTART+i];
        [imagview.layer addAnimation:[self moveanimation:TAGSTART+i number:t] forKey:@"position"];
        [imagview.layer addAnimation:[self setscale:TAGSTART + i clicktag:tag] forKey:@"transform"];
        
        int j = array[tag - TAGSTART][i];
        float Scalenumber = fabs(j - PHOTONUM/2.0)/(PHOTONUM/2.0);

        if (Scalenumber<0.2)
        {
            
            Scalenumber = 0.5;
        }
        
        
}
    if (t == 6) {
        CommonViewController *common = [[CommonViewController alloc] init];
        common.categoryID = 4;
            
        [self.navigationController pushViewController:common animated:YES];
        [common release];
    }else if (t == 1){
       
        CommonViewController *common = [[CommonViewController alloc] init];
        common.categoryID = 2;
        [self.navigationController pushViewController:common animated:YES];
        [common release];
    }else if (t == 2){
        
        CommonViewController *common = [[CommonViewController alloc] init];
        common.categoryID = 3;
        [self.navigationController pushViewController:common animated:YES];
        [common release];
    }else if (t == 3){
       
            
        CommonViewController *common = [[[CommonViewController alloc] init] autorelease];
        common.categoryID = 8;
        [self.navigationController pushViewController:common animated:YES];
       
        
    }else if(t == 4){
        
        CommonViewController *common = [[[CommonViewController alloc] init] autorelease];
        common.categoryID = 5;
        [self.navigationController pushViewController:common animated:YES];
        
    }else if(t == 5){
        
        CommonViewController *common = [[[CommonViewController alloc] init] autorelease];
        common.categoryID = 7;
        [self.navigationController pushViewController:common animated:YES];
       
    }
    
    
    
    
}


- (CAAnimation *)setscale:(NSInteger)tag clicktag:(NSInteger)clicktag
{
    int i = array[clicktag - TAGSTART][tag - TAGSTART];
    int i1 = array[currenttag - TAGSTART][tag - TAGSTART];
    float Scalenumber = fabs(i - PHOTONUM/2.0)/(PHOTONUM/2.0);
    float Scalenumber1 = fabs(i1 - PHOTONUM/2.0)/(PHOTONUM/2.0);
    if (Scalenumber<0.3)
    {
        Scalenumber = 0.7;
    }
    //    UIImageView *imgview = (UIImageView*)[self.view viewWithTag:tag];
    animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.duration = TIME;
    animation.repeatCount =1;
    
    
    CATransform3D dtmp = CATransform3DScale(rotationTransform1[tag - TAGSTART],Scalenumber*SCALENUMBER, Scalenumber*SCALENUMBER, 1.0);
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DScale(rotationTransform1[tag - TAGSTART],Scalenumber1*SCALENUMBER,Scalenumber1*SCALENUMBER, 1.0)];
    animation.toValue = [NSValue valueWithCATransform3D:dtmp ];
    animation.autoreverses = NO;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    
    
    return animation;
}

- (CAAnimation *)moveanimation:(NSInteger)tag number:(NSInteger)num
{
    UIImageView *imgview = (UIImageView*)[self.view viewWithTag:tag];
//     animation;
    CAKeyframeAnimation* animation1 = [CAKeyframeAnimation animation];
    CGMutablePathRef path = CGPathCreateMutable();
    NSLog(@"原点%f原点%f",imgview.layer.position.x,imgview.layer.position.y);
    CGPathMoveToPoint(path, NULL,imgview.layer.position.x,imgview.layer.position.y);
    
    NSInteger p = [self getBlank:tag];
    NSLog(@"旋转%ld",p);
    float f = 2.0*M_PI  - 2.0*M_PI *p/PHOTONUM;
    float h = f + 2.0*M_PI *num/PHOTONUM;
    float centery = self.view.center.y - 50;
    float centerx = self.view.center.x;
    float tmpy =  centery + RADIUS*cos(h);
    float tmpx =	centerx - RADIUS*sin(h);
    imgview.center = CGPointMake(tmpx,tmpy);
    
    CGPathAddArc(path,nil,self.view.center.x, self.view.center.y - 50,RADIUS,f+ M_PI/2,f+ M_PI/2 + 2.0*M_PI *num/PHOTONUM,0);
    animation1.path = path;
    CGPathRelease(path);
    animation1.duration = TIME;
    animation1.repeatCount = 1;
    animation1.calculationMode = @"paced";
    return animation1;
}

- (NSInteger)getBlank:(NSInteger)tag
{
    if (currenttag>tag)
    {
        return currenttag - tag;
    }
    else
    {
        return PHOTONUM  - tag + currenttag;
    }
    
}

- (void)Scale
{
    [UIView beginAnimations:nil context:(__bridge void *)(self)];
    [UIView setAnimationRepeatCount:3];
    [UIView setAnimationDuration:1];
    
    CATransform3D rotationTransform = CATransform3DIdentity;
    
    rotationTransform = CATransform3DRotate(rotationTransform,3.14, 1.0, 0.0, 0.0);
    
    self.view.layer.transform=rotationTransform;
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
